package com.springBoot.service.impl;

import com.springBoot.commonClass.Utils;
import com.springBoot.entity.RoleEntity;
import com.springBoot.entity.UserEntity;
import com.springBoot.entity.UserRoleMapperEntity;
import com.springBoot.entity.union.UserWithRoleEntity;
import com.springBoot.exception.BusinessFailureException;
import com.springBoot.repository.RoleRepository;
import com.springBoot.repository.UserRepository;
import com.springBoot.repository.UserRoleMapperRepository;
import com.springBoot.repository.union.UserWithRoleRepository;
import com.springBoot.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserWithRoleRepository userWithRoleRepository;
    @Autowired
    private UserRoleMapperRepository userRoleMapperRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Override
    public List<Map<String, String>> retrieveByParams(UserEntity userEntity) {
        List<Map<String, String>> res = new ArrayList<>();
        List<UserEntity> list = userRepository.selectByParams(userEntity);
        for(UserEntity user: list){
            Map<String, String> userInfo = new HashMap<>();
            userInfo.put("userName", user.getUserName());
            userInfo.put("userId", user.getUserId());
            userInfo.put("orgId", user.getOrgId());
            res.add(userInfo);
        }
       return res;
    }

    @Override
    public Map<String, String> getUserByUserAcc(String userAcc) {
        UserEntity user = userRepository.getUserByAcc(userAcc);
        Map<String, String> userRes = null;
        if(user != null){
            userRes = new HashMap<>();
            userRes.put("userId", user.getUserId());
            userRes.put("userName", user.getUserName());
            userRes.put("userAcc", user.getUserAcc());
            userRes.put("userPwd", user.getUserPwd());
        }
        return userRes;
    }

    @Override
    public UserWithRoleEntity login(String userAcc, String userPwd) throws Exception {
//        UserEntity user = userRepository.login(userAcc, userPwd);
        UserWithRoleEntity user = userWithRoleRepository.login(userAcc, userPwd);
        if(user == null){
//            throw new Exception("账号密码错误");
            throw new BusinessFailureException(true);
        }
        return user;
    }

    @Override
    public Map<String, String> getUserByUserId(String userId) {
        UserEntity user = userRepository.getUserById(userId);
        Map<String, String> userRes = null;
        if(user != null){
            userRes = new HashMap<>();
            userRes.put("userId", user.getUserId());
            userRes.put("userName", user.getUserName());
            userRes.put("userAcc", user.getUserAcc());
            userRes.put("userPwd", user.getUserPwd());
        }
        return userRes;
    }

    @Override
    public UserWithRoleEntity getUserInfoByUserId(String userId) {
        UserWithRoleEntity user = userWithRoleRepository.getUserInfoByUserId(userId);
        if(user == null) {
            throw new BusinessFailureException(true);
        }
        return user;
    }

    @Override
    public boolean updateUserInfo(UserWithRoleEntity userWithRoleEntity) {
        UserEntity userEntity = new UserEntity();
        UserRoleMapperEntity userRoleMapperEntity = new UserRoleMapperEntity();
        BeanUtils.copyProperties(userWithRoleEntity, userEntity);
        BeanUtils.copyProperties(userWithRoleEntity, userRoleMapperEntity);
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRoleName(userWithRoleEntity.getRoleName());
        roleEntity = roleRepository.selectByParams(roleEntity).get(0);
        userRoleMapperEntity.setRoleId(roleEntity.getRoleId());
        Integer res1 = userRepository.update(userEntity);
        Integer res2 = userRoleMapperRepository.update(userRoleMapperEntity);
        boolean isUpdateSuccess = true;
        if (res1 == 0 || res2 == 0){
            isUpdateSuccess = false;
        }
        System.out.println(res1+"****"+res2);
        return isUpdateSuccess;
    }

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public Future<UserWithRoleEntity> insertNewUserInfo(UserWithRoleEntity userWithRoleEntity) {
        UserEntity userEntity = new UserEntity();
        userWithRoleEntity.setUserId(Utils.getUuid());
        userWithRoleEntity.setOrgId(userWithRoleEntity.getUserId());
        userWithRoleEntity.setCreateDateTime(LocalDateTime.now());
        userWithRoleEntity.setLastModifiedDateTime(LocalDateTime.now());
        userWithRoleEntity.setDeleteFlag(false);
        BeanUtils.copyProperties(userWithRoleEntity, userEntity);
            //插入user表信息
            userRepository.insert(userEntity);
            //查询roleId;
            RoleEntity params = new RoleEntity();
            params.setRoleName(userWithRoleEntity.getRoleName());
            RoleEntity roleEntity = roleRepository.selectByParams(params).get(0);
            //插入mapper表
            UserRoleMapperEntity mapperEntity = new UserRoleMapperEntity();
            BeanUtils.copyProperties(userWithRoleEntity, mapperEntity);
            mapperEntity.setRoleId(roleEntity.getRoleId());
            userRoleMapperRepository.insert(mapperEntity);
//            userWithRoleEntity.setUserId(userEntity.getUserId());
            return new AsyncResult<>(userWithRoleEntity);
    }

    @Override
    public void deleteUser(String userId, String lastModifiedBy) {
        UserEntity userEntity = new UserEntity();
        LocalDateTime now = LocalDateTime.now();
        userEntity.setUserId(userId);
        userEntity.setLastModifiedDateTime(now);
        userEntity.setLastModifiedBy(lastModifiedBy);
        UserRoleMapperEntity mapperEntity = new UserRoleMapperEntity();
        mapperEntity.setUserId(userId);
        mapperEntity.setLastModifiedBy(lastModifiedBy);
        mapperEntity.setLastModifiedDateTime(now);
        userRepository.delete(userEntity);
        userRoleMapperRepository.delete(mapperEntity);
    }
}
